package com.migo.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wes on 2016-09-23.
 */
public class HttpClientUtils {

    public static String SMSpost(String url, String params, String appkey) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("params", params));
        formparams.add(new BasicNameValuePair("appkey", appkey));
        return new HttpClientUtils().request(formparams, url);
    }

    public String request(List<NameValuePair> formparams, String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPost.setEntity(uefEntity);
            System.out.println("executing request " + httpPost.getURI());
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpPost);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity, "UTF-8");
                }
            } finally {
                if(response!=null)
                {
                    response.close();
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String GET(String wxurl) throws IOException {
        URL url = new URL(wxurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines = reader.readLine();
        reader.close();
        return lines;
    }

    public static String sendMail(String url, String email,String code) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("email", email));
        formparams.add(new BasicNameValuePair("code", code));
        return new HttpClientUtils().request(formparams, url);
    }

    public static String post(String url, String requestJson) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("params", requestJson));
        return new HttpClientUtils().request(formparams, url);
    }

    public static String Post(String url, String requestJson, String language) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("params", requestJson));
        formparams.add(new BasicNameValuePair("language", language));
        return new HttpClientUtils().request(formparams, url);
    }

    public static String PostCharset(String url, String requestJson, String charset) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("params", requestJson));
        formparams.add(new BasicNameValuePair("charset", charset));
        return new HttpClientUtils().request(formparams, url);
    }

    public static String wxGet(String wxurl) throws IOException {
        URL url = new URL(wxurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines = reader.readLine();
        System.out.println("lines:" + lines);
        reader.close();
        return lines;
    }

    public static void main(String[] args){
        String url = "http://api.sms.powervision.me/api/sms/sendAliyunEmail";
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("email", "wes.wang@powervision.me"));
        formparams.add(new BasicNameValuePair("code", "456"));
        formparams.add(new BasicNameValuePair("ip", "1.1.1.1"));
        String result = new HttpClientUtils().request(formparams, url);
        System.out.println(result);
    }
}
